package com.ljkj.zjf.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.ljkj.access.Tlog;
import com.ljkj.access.Trole;
import com.ljkj.access.Tuser;
import com.ljkj.common.db.dao.TLogDAO;
import com.ljkj.common.db.util.DateTime;

public class TLogService {
	
	private TLogDAO tLogDAO;
	private TuserService tuserService;
	



	/**
	 * 获取操作日志列表
	 * @param params 权限名称关键字
	 * @param currentPage 当前页
	 * @param pageSize 每页条数
	 * @param sortField 排序字段
	 * @param sortOrder 排序方式
	 * @return
	 */
	public String getTlogByProperties(String params,int currentPage,int pageSize,String sortField,String sortOrder){
 
		Class[] pojos = {Tlog.class};
		String[] propertyNames=null;
		Object[] values=null;
		String[] likePropertyNames = null;
		Object[] likevalues= null;
		String[] betweenPropertyNames=null;
		Object[][]  betweenvalues=null;
		String[] orderByPropertyNames=null;
		Object[] orderByValues=null;
		
		if(sortField != null){
			orderByPropertyNames = new String[]{sortField};
			orderByValues =  new String[]{sortOrder};
		}

		int count = this.tLogDAO.getCount(pojos, propertyNames, values,likePropertyNames, likevalues, betweenPropertyNames,betweenvalues);
		this.tLogDAO.setPageMaxCount(pageSize);
		List<Tlog> tlogs = this.tLogDAO.findByProperties(pojos, propertyNames, values, likePropertyNames, likevalues, betweenPropertyNames, betweenvalues, orderByPropertyNames, orderByValues, currentPage);
		
		List<Object> entry_list = new ArrayList<Object>();
		//迭代遍历所有条目，放到map里
		for(Tlog tlog : tlogs){
			
			Map<String,Object> entry_map = new HashMap<String,Object>();
			entry_map.put("logId",tlog.getLogId());
			entry_map.put("opType", tlog.getOpType());
			if(tlog.getTuId() != null){
				Tuser tuser = this.tuserService.getUserById(tlog.getTuId());
				entry_map.put("tuserName", tuser.getLoginName());
			}
			entry_map.put("content",tlog.getContent());
			entry_map.put("createTime",tlog.getCreateTime());
			entry_map.put("createUser", tlog.getCreateUser());
			entry_map.put("updateTime", tlog.getUpdateTime());
			entry_map.put("updateUser", tlog.getUpdateUser());
			entry_map.put("disabledState", tlog.getDisabledState());
			
			entry_list.add(entry_map);
		}
		
		HashMap<String,Object> result_map = new HashMap<String, Object>();
		result_map.put("data", entry_list);
		result_map.put("total", count);
		
        return JSON.toJSONString(result_map);
	}
	
	
	
	/**
	 * 新增或更新日志
	 * @param zdgCoupon
	 * @return
	 */
	public String addOrEditTlogInfo(Tlog tlog,Tuser tuser){
		
		tlog.setTuId(tuser.getTuId());
		
		Map<String,String> result_map = new HashMap<String, String>();
		if(tlog.getLogId() == null){
			tlog.setCreateTime(DateTime.getDateTime());
			tlog.setCreateUser(tuser.getLoginName());
			if(this.saveNewTlog(tlog)){
				result_map.put("err_msg", "新增成功。");
				result_map.put("err_code", "0");
			}else{
				result_map.put("err_msg", "新增失败。");
				result_map.put("err_code", "1");
			}
			
		}
		
		return JSON.toJSONString(result_map);
	}
	
	public boolean saveNewTlog(Tlog tlog){
		try{
			this.tLogDAO.save(tlog);
			return true;
		}catch(Exception ex){
			return false;
		}
	}
	
	public Trole getTlogById(Integer id){
		return this.getTlogById(id);
	}
	
	//注入DAO
	public void settLogDAO(TLogDAO tLogDAO) {
		this.tLogDAO = tLogDAO;
	}
	
	public void setTuserService(TuserService tuserService) {
		this.tuserService = tuserService;
	}
}
